//
//  NotBoundUserVC.m
//  AILock
//
//  Created by 谭亚明 on 2020/7/13.
//  Copyright © 2020 tym. All rights reserved.
//

#import "NotBoundUserVC.h"
#import "GroupShadowTableView.h"
#import "NotBoundUseCell.h"
#import "Sizes.h"
#import "LYEmptyViewHeader.h"
#import "EquipSettingVC.h"
#import "EquipmentService.h"

@interface NotBoundUserVC ()<GroupShadowTableViewDelegate, GroupShadowTableViewDataSource>

@property (nonatomic, strong) GroupShadowTableView *tableView;
@property(nonatomic,strong) EquipmentService *service;
@property(nonatomic,strong) EquipmentModel *equipment;
@property(nonatomic,strong) EquUsers *users;
@end

@implementation NotBoundUserVC

-(instancetype)initEquipmentModel:(EquipmentModel*)model{
    self = [super init];
    self.equipment = model;
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTable];
    
    
}

-(void)zj_viewWillAppearForIndex:(NSInteger)index{
    
    [self getEquipmentUsers];
}

-(EquipmentService*)service{
    if (_service == nil) {
        _service = [[EquipmentService alloc] init];
    }
    return _service;
}

-(void)getEquipmentUsers{
    TYMWeak(self);
    [self.service getEquipmentuserlistgroupallEquipment:_equipment.equipmentId type:3 completion:^(LMJBaseResponse * _Nonnull respons, EquUsers *users) {
        
        weakself.tableView.isShowArray = @[@(users.pwdList.count),@(users.fingerprintList.count),@(users.cardList.count),@(users.remoteControlList.count),];
        weakself.users = users;
        [weakself.tableView reloadData];
        
        if (!users.pwdList.count &&
            !users.fingerprintList.count&&
            !users.cardList.count&&
            !users.remoteControlList.count) {
            [self.tableView ly_showEmptyView];
        }
    }];
}

- (void)setTable {
    self.tableView = [[GroupShadowTableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-NavigateonHeight-SafeBottomMargin) style:UITableViewStyleGrouped];
    self.tableView.headTitles = @[@"密码",@"指纹",@"卡",@"遥控"];
    self.tableView.ly_emptyView = [TYMEmptyView emptyViewWithImageStr:@"empty_img"
     titleStr:@"还没有未绑定哦"
    detailStr:@""];
    self.tableView.groupShadowDelegate = self;
    self.tableView.groupShadowDataSource = self;
    self.tableView.backgroundColor = viewBGColor;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0.01)];
    self.tableView.sectionFooterHeight = 0;
    _tableView.showSeparator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
}

#pragma mark delegate datasource
- (NSInteger)numberOfSectionsInGroupShadowTableView:(GroupShadowTableView *)tableView {
    return 4;
}

- (NSInteger)groupShadowTableView:(GroupShadowTableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0){
        return _users.pwdList.count;
    }else if(section == 1){
        return _users.fingerprintList.count;
    }else if(section == 2){
        return _users.cardList.count;
    }
    return _users.remoteControlList.count;
}

- (CGFloat)groupShadowTableView:(GroupShadowTableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [NotBoundUseCell cellForHeight];
}

- (UITableViewCell *)groupShadowTableView:(GroupShadowTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NotBoundUseCell *cell = [tableView dequeueReusableCellWithIdentifier:[NotBoundUseCell identifier]];
    if (!cell) {
        cell = [[NotBoundUseCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[NotBoundUseCell identifier]];
    }
    if(indexPath.section == 0){
        cell.equUserModel = _users.pwdList[indexPath.row];
    }else if(indexPath.section == 1){
        cell.equUserModel = _users.fingerprintList[indexPath.row];
    }else if(indexPath.section == 2){
        cell.equUserModel = _users.cardList[indexPath.row];
    }
    [cell hiddenRightImage];
    return cell;
}

-(void)groupShadowTableView:(GroupShadowTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    EquipmentUserModel *model;
    if(indexPath.section == 0){
        model =  _users.pwdList[indexPath.row];
    }else if(indexPath.section == 1){
        model = _users.fingerprintList[indexPath.row];
    }else if(indexPath.section == 2){
        model =  _users.cardList[indexPath.row];
    }

    EquipSettingVC *settingVC = [[EquipSettingVC alloc] initWithEquipmentUserID:model.id];

    [self.navigationController pushViewController:settingVC animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
